<?php
/**
 * @version		$Id: profile.php 20196 2011-01-09 02:40:25Z ian $
 * @package		Joomla.Site
 * @subpackage	com_users
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT.'/controller.php';

class ShopControllerCart extends ShopController
{
	public function update()
	{
		$app			= JFactory::getApplication();
		$session = JFactory::getSession();
		$carts  = $session->get('lightcart');
		$quantity    = JRequest::getVar('quantity', array(), '', 'array');
	 	foreach($quantity as $key=>$value){
			$carts[$key]['quantity'] = $value;
		}
		$session->set('lightcart',$carts);

		$this->setRedirect(JRoute::_('index.php?option=com_shop&view=cart&layout=default', false));
	}
	function delete()
	{
		$id = (int)JRequest::getVar('productid','');
		if($id != ''){
			$session = JFactory::getSession();
			$carts  = $session->get('lightcart');
			unset($carts[$id]);
			$session->set('lightcart',$carts);
			$this->setRedirect(JRoute::_('index.php?option=com_shop&view=cart&layout=default', false));
		}else{
			$this->setRedirect(JRoute::_('index.php?option=com_shop&view=cart&layout=default'),'Error update cart');
		}
	}
	function complete()
	{
		JPluginHelper::importPlugin('content');
		$dispatcher	= JDispatcher::getInstance();
		if (!class_exists('plgContentLightShop')) {
			JError::raiseWarning(50, str_rot13('WYVO_FUBC_REEBE_YBNQ_CYHTVA'));
		}

		$name = JRequest::getVar('username');
		$email = JRequest::getVar('email');
		$phone = JRequest::getVar('phone');
		$province = JRequest::getVar('province'); // id 
		$district = JRequest::getVar('district'); // id
		$address2 = JRequest::getVar('address');
		$method_id = JRequest::getVar('method',0); // id
		
		
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		// get address prodinv
		$query->select('d1.title, d2.title as parent');
		$query->from('#__shop_address as d1,#__shop_address as d2');
		$query->where("d1.id = $district and d2.id = d1.parent_id");
		$db->setQuery((string)$query);
		$address1 = $db->loadObject();
		// get method 
		$query->clear();
		$query->select('*');
		$query->from('#__shop_method');
		$query->where('id = '.$method_id);
		$db->setQuery((string)$query);
		$method = $db->loadObject();
		
		// get session cart 
		$session  = JFactory::getSession();
		$carts = $session->get('lightcart');
$path = JURI::root().'images/image_product/thumb/';		
		$content = '';
		$content .='<style>.title_cart_product{text-align: center;font-weight: bold;background: #ADF;padding: 2px;}.img_cart{ width:70px; height:70px; margin:5px;border-radius: 5px;}.title_product_cart{font-size: 14px;text-align: center;}.title_product_price{color: #690707;text-align: center;font-weight: bold;}.total-f-price, .total_price{text-align: center;color: red;font-weight: bold;}.total-f-price-text{text-align: center;color: blue;font-weight: bold;}</style>';
		
	$content .='<div class="title_cart"><h2><a href="'.JURI::root().'" target="_blank">'.JText::_('ORDER CART ON ').':'.JURI::root().'</a></h2></div>';
	$content .='<table border="1" width="100%">
<thead>
<tr>
    	<td class="title_cart_product">'.JText::_('PICTURE').'</td><td class="title_cart_product">'.JText::_('PRODUCT').'</td><td class="title_cart_product">'.JText::_('PRICE').'</td><td class="title_cart_product">'.JText::_('QUANTITY').'</td><td class="title_cart_product">'.JText::_('TOTAL PRICE').'</td>
    </tr>
</thead>
<tbody>';
foreach($carts as $cart){
$dispatcher->trigger('onFormatPrice',array($cart['price'],$cart['currency'],&$price));
$totalprice = $cart['price']*$cart['quantity'];
$dispatcher->trigger('onFormatPrice',array($totalprice,$cart['currency'],&$totalprice));
$content .='<tr>
    	<td align="center">
        <img src="'.$path.$cart['image'].'" title="'.$cart['title'].'" width="100" height="100" class="img_cart" /></td>
        <td class="title_product_cart">'.$cart['title'].'</td>
        <td class="title_product_price">'.$price.'</td>
        <td align="center">'.$cart['quantity'].'</td>
        <td class="total_price">'.$totalprice.'
        </td>
    </tr>';
}
$content .='</tbody>';
$total = 0;
$currency = '';
foreach($carts as $cart){
	$total +=$cart['price']*$cart['quantity'];
	$currency = $cart['currency'];
}
$dispatcher->trigger('onFormatPrice',array($total,$currency,&$total)); 
$content .='<tfoot>
	<tr>
    	<td colspan="3"></td>
        <td class="total-f-price-text">'.JText::_('TOTAL PRICE').'</td>
        <td class="total-f-price">'.$total.'</td>
    </tr>
</tfoot>';
$content .='</table>';

$content .='<div class="title-customer"><h3>'.JText::_('CUSTOMER INFOMATION').'</h3></div>';
$content .='<ul>
	<li><span>'.JText::_('NAME').':</span> '.$name.'</li>
    <li><span>'.JText::_('EMAIL').':</span> '.$email.'</li>
    <li><span>'.JText::_('PHONE').':</span> '.$phone.'</li>
    <li><span>'.JText::_('ADDRESS').':</span> '.$address2.', '.$address1->title.', '.$address1->parent.'</li>
    <li><span>'.JText::_('METHOD').':</span> '.$method->text.'</li>
</ul>';

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
$row = JTable::getInstance('Order','ShopTable');

$row->username = $name;
$row->email = $email;
$row->phone = $phone;
$row->province = $province;
$row->district = $district;
$row->address = $address2;
$row->content = $content;
$row->createdate = date('Y-m-d H:i:s');
$row->viewed = 0;
$row->delivery = 0;
$row->payed = 0;
$row->code = time();

// set session 
		$session = JFactory::getSession();
		$cus = $session->get('customer');
		$cus['username'] = $name;
		$cus['email'] = $email;
		$cus['phone'] = $phone;
		$cus['address'] = $address2.', '.$address1->title.', '.$address1->parent;
		$session->set('customer',$cus);
// end set session

$app        = JFactory::getApplication();
$mailfrom    = $app->getCfg('mailfrom');
$fromname    = $app->getCfg('fromname');
$sitename    = $app->getCfg('sitename');

 $mail = JFactory::getMailer();
 $mail->addRecipient($mailfrom);
 $mail->addCC($email);
//    $mail->setSender(array($mailfrom , $fromname));
 $mail->setSubject(JText::_('ORDER FROM').": ".$sitename);
 $mail->setBody($content);
 $mail->IsHTML(true);
 $mail->Send();

if($row->store()){
	$this->setRedirect(JRoute::_('index.php?option=com_shop&view=cart&layout=complete'));
}else{
	$this->setRedirect(JRoute::_('index.php'),JText::_('ERROR ORDER PRODUCT'));
}

	}
}
